<?php

App::uses('AppController', 'Controller');

class ClientsController extends AppController {
    
    public $pageTitle = "Klijenti";
    
    public $paginate = array(
        'limit' => ITEMS_PER_PAGE
    );
    
    public $uses = array('Client');

    public function index() {
        $filter = $this->parseFilterData('Client');
        
        $this->paginate['order'] = "{$filter->sortBy} {$filter->sortDir}";
        $clients = $this->paginate('Client', $filter->conditions);
        
        $this->set(compact('clients'));
        $this->setFilterData($filter);
    }
    
    public function create() {
        $data = $this->Client->create();
        
        $this->set(compact('data'));
        $this->render('form');
    }
    
    public function edit($id) {
        $data = $this->Client->findById($id);
        
        $this->set(compact('data'));
        $this->render('form');
    }
    
    public function save() {
        $data = $this->data;
        if (! empty($data)) {
            $this->Client->create($data);
            
            if (! $this->Client->validates()) {
                $this->setFlash('Uneti podaci nisu validni!');
                $this->set('data', $data);
                return $this->render('form');
            }
            
            if (! $this->Client->save($data)) {
                $this->setFlash('Čuvanje nije uspelo!');
                $this->set('data', $data);
                return $this->render('form');
            }
            
            $this->setFlash('Klijent je sačuvan.', 'ok');
            $this->redirect('edit/' . $this->Client->id);
        }
        $this->redirect('index');
    }
    
    public function remove($id) {
        $this->setFlash('Klijent je obrisan.', 'ok');
        if (! $this->Client->delete($id)) {
            $this->setFlash('Klijent nije obrisan!');
        }
        $this->redirect("index");
    }
    
}
